/* *************************** (C) COPYRIGHT 2015 Fortiortech shenzhen *****************************
    File Name          : BuzzerScan.c
    Author             : Billy Long Fortiortech  Market Dept
    Version            : V1.0
    Date               : 01/07/2015
    Description        : This file contains Buzzer Scan function used for Motor Control.
***************************************************************************************************
    All Rights Reserved
**************************************************************************************************/

#include <FU68xx_4.h>
#include <MyProject.h>

BZScan_TypeDef xdata BZScan;
MCLedDisplay xdata mcLedDisplay;

/*  -------------------------------------------------------------------------------------------------
    Function Name : void BuzzerInit(void)
    Description   : 按键参数初始化
    Input         : 无
    Output        : 无
    -------------------------------------------------------------------------------------------------*/
void BuzzerInit(void)
{
    BZScan.FlagBZ = 0;
    BZScan.BZRunTime = 0;
    BZScan.BZCnt = 0;
    BZScan.BZMultitimes = 0;
}

/*  -------------------------------------------------------------------------------------------------
    Function Name : void SetBuzzer(uint8 Length)
    Description   : 置位蜂鸣器鸣叫标志
    Input         : Length：响铃长度  单位：100ms
    Output        : 无
    -------------------------------------------------------------------------------------------------*/
void SetBuzzer(uint8 Flag, uint8 BZRunTime, uint8 BZMultitimes)
{
    BZScan.FlagBZ = Flag;
    BZScan.BZRunTime = BZRunTime;
    BZScan.BZMultitimes = BZMultitimes;
}

/*  ----------------------------------------------------------------------------------------------*/
/*  Function Name  : BuzzerScan
/*  Description    : 蜂鸣器，BZScan.BZRunTime控制时间长度
/*  Date           : 2020-03-25
/*  Parameter      : None
/*  ----------------------------------------------------------------------------------------------*/
void BuzzerScan(void)
{
    if (BZScan.FlagBZ && BZScan.BZMultitimes)
    {
        if (BZScan.BZMultitimes > 0)
        {
            if (++BZScan.BZCnt <= (200 * BZScan.BZRunTime))
            {
                GP10 = ~GP10;
            }
            else if (BZScan.BZCnt <= (400 * BZScan.BZRunTime))
            {
                GP10 = 0;
                BZScan.BZMultitimes--;
                BZScan.BZCnt = 0;
            }
        }
    }
}

/*---------------------------------------------------------------------------*/
/*  Name     :   void Led_OnOff(void)
/* Input    :   NO
/* Output   :   NO
/* Description: LED灯的控制
/*---------------------------------------------------------------------------*/
void Led_OnOff(MCLedDisplay *hLedDisplay, uint8 htime)
{
    if (++hLedDisplay->LedCount < 7000)
    {
        if (hLedDisplay->Count < hLedDisplay->Counttime)
        {
            hLedDisplay->Count++;
        }
        else if ((hLedDisplay->Count >= hLedDisplay->Counttime) &&
                 (hLedDisplay->Count < (hLedDisplay->Counttime + 1)))
        {
            hLedDisplay->Count = 0;
            LEDPinONOFF;
            hLedDisplay->LedTimCot++;
        }
        else
            ;

        if (hLedDisplay->LedTimCot >= 2 * htime)
        {
            hLedDisplay->Count = 302;
            mcLedDisplay.FlagOver = 1;
        }
    }
    else if (hLedDisplay->LedCount >= 7000)
    {
        hLedDisplay->LedCount = 0;
        hLedDisplay->LedTimCot = 0;
        hLedDisplay->Count = 0;
    }
}

/*---------------------------------------------------------------------------*/
/*  Name     :   void LED_Display(void)
/* Input    :   NO
/* Output   :   NO
/* Description: LED灯显示
/*---------------------------------------------------------------------------*/
void LED_Display(void)
{
    if (mcLedDisplay.FlagOver != 0)
        return;

    switch (mcLedDisplay.LedFlashNum)
    {
    case 0xF1: // 开关机、上电后 LED状态指示
        mcLedDisplay.LedCount++;
        if (IRControl.LEDONOFFStatus == 1)
        {
            if (mcLedDisplay.LedCount < mcLedDisplay.Counttime) //
            {
                SetLEDPin; ////ON
            }
            else
            {
                ResetLEDPin; ////OFF
                mcLedDisplay.FlagOver = 1;
            }
        }
        else
        {
            if (mcLedDisplay.LedCount < mcLedDisplay.Counttime)
            {
                ResetLEDPin;
            }
            else
            {
                SetLEDPin;
                mcLedDisplay.FlagOver = 1;
            }
        }
        break;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
        Led_OnOff(&mcLedDisplay, mcLedDisplay.LedFlashNum);
        break;
    default:
        break;
    }
}

void Set_LED(uint16 tLED_OnTime, uint8 tFlashTimes)
{
    mcLedDisplay.Counttime = tLED_OnTime;
    mcLedDisplay.LedFlashNum = tFlashTimes;
    mcLedDisplay.LedTimCot = 0;
    mcLedDisplay.LedCount = 0;
    mcLedDisplay.Count = 0;
    mcLedDisplay.FlagOver = 0;
}
